// 缺陷管理相关的类型定义和常量

// 缺陷优先级枚举 - 与后端保持一致
export enum DefectPriority {
  LOWEST = 'lowest',      // 最低
  LOW = 'low',            // 低
  MEDIUM = 'medium',      // 中
  HIGH = 'high',          // 高
  URGENT = 'urgent',      // 紧急
  CRITICAL = 'critical',  // 严重
  BLOCKER = 'blocker'     // 阻塞
}

// 缺陷严重程度枚举 - 与后端保持一致
export enum DefectSeverity {
  TRIVIAL = 'trivial',      // 轻微
  MINOR = 'minor',          // 次要
  MAJOR = 'major',          // 主要
  IMPORTANT = 'important',  // 重要
  CRITICAL = 'critical',     // 严重
  URGENT = 'urgent',        // 紧急
  BLOCKER = 'blocker'       // 阻塞
}

// 缺陷类型枚举 - 与后端保持一致
export enum DefectType {
  FUNCTIONAL = 'functional',        // 功能缺陷
  UI = 'ui',                        // 界面缺陷
  PERFORMANCE = 'performance',      // 性能问题
  SECURITY = 'security',            // 安全问题
  COMPATIBILITY = 'compatibility',   // 兼容性问题
  USABILITY = 'usability',          // 易用性问题
  DATA = 'data',                    // 数据问题
  INTEGRATION = 'integration',     // 集成问题
  CONFIGURATION = 'configuration',   // 配置问题
  DOCUMENTATION = 'documentation'   // 文档问题
}

// 缺陷来源枚举 - 与后端保持一致
export enum DefectSource {
  TESTING = 'testing',              // 测试
  PRODUCTION = 'production',        // 生产环境
  CODE_REVIEW = 'code_review',      // 代码审查
  USER_FEEDBACK = 'user_feedback',  // 用户反馈
  MONITORING = 'monitoring',        // 监控发现
  INTERNAL = 'internal',            // 内部发现
  EXTERNAL = 'external'             // 外部发现
}

// 缺陷状态选项 - 已迁移到动态状态管理
// 使用 useDefectStates composable 获取状态选项
// 保留此导出仅为向后兼容，建议使用 useDefectStates() 获取状态选项
export const DEFECT_STATUS_OPTIONS: Array<{ value: string; label: string; color: string }> = []

// 缺陷优先级选项
export const DEFECT_PRIORITY_OPTIONS = [
  { value: 'lowest', label: '最低', color: '#C0C4CC' },
  { value: 'low', label: '低', color: '#909399' },
  { value: 'medium', label: '中等', color: '#E6A23C' },
  { value: 'high', label: '高', color: '#F56C6C' },
  { value: 'urgent', label: '紧急', color: '#F56C6C' },
  { value: 'critical', label: '严重', color: '#F56C6C' },
  { value: 'blocker', label: '阻塞', color: '#F56C6C' }
] as const

// 缺陷严重程度选项
export const DEFECT_SEVERITY_OPTIONS = [
  { value: 'trivial', label: '轻微', color: '#C0C4CC' },
  { value: 'minor', label: '次要', color: '#909399' },
  { value: 'major', label: '主要', color: '#E6A23C' },
  { value: 'important', label: '重要', color: '#F56C6C' },
  { value: 'critical', label: '严重', color: '#F56C6C' },
  { value: 'urgent', label: '紧急', color: '#F56C6C' },
  { value: 'blocker', label: '阻塞', color: '#F56C6C' }
] as const

// 缺陷类型选项
export const DEFECT_TYPE_OPTIONS = [
  { value: 'functional', label: '功能性', icon: 'el-icon-setting' },
  { value: 'ui', label: '界面', icon: 'el-icon-picture' },
  { value: 'performance', label: '性能', icon: 'el-icon-timer' },
  { value: 'security', label: '安全', icon: 'el-icon-lock' },
  { value: 'compatibility', label: '兼容性', icon: 'el-icon-connection' },
  { value: 'usability', label: '可用性', icon: 'el-icon-user' },
  { value: 'data', label: '数据', icon: 'el-icon-document' },
  { value: 'integration', label: '集成', icon: 'el-icon-link' },
  { value: 'configuration', label: '配置', icon: 'el-icon-tools' },
  { value: 'documentation', label: '文档', icon: 'el-icon-document' }
] as const

// 缺陷来源选项
export const DEFECT_SOURCE_OPTIONS = [
  { value: 'testing', label: '测试', icon: 'el-icon-cpu' },
  { value: 'production', label: '生产环境', icon: 'el-icon-monitor' },
  { value: 'code_review', label: '代码审查', icon: 'el-icon-view' },
  { value: 'user_feedback', label: '用户反馈', icon: 'el-icon-chat-dot-round' },
  { value: 'monitoring', label: '监控发现', icon: 'el-icon-warning' },
  { value: 'internal', label: '内部发现', icon: 'el-icon-office-building' },
  { value: 'external', label: '外部发现', icon: 'el-icon-position' }
] as const

// 缺陷状态流转规则已迁移到状态管理系统
// 使用状态管理系统中的状态流转规则

// 缺陷表单验证规则
export const DEFECT_FORM_RULES = {
  title: [
    { required: true, message: '请输入缺陷标题', trigger: 'blur' },
    { min: 5, max: 200, message: '标题长度应在 5 到 200 个字符', trigger: 'blur' }
  ],
  description: [
    { required: true, message: '请输入缺陷描述', trigger: 'blur' },
    { min: 10, max: 2000, message: '描述长度应在 10 到 2000 个字符', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择缺陷类型', trigger: 'change' }
  ],
  priority: [
    { required: true, message: '请选择优先级', trigger: 'change' }
  ],
  severity: [
    { required: true, message: '请选择严重程度', trigger: 'change' }
  ],
  source: [
    { required: true, message: '请选择缺陷来源', trigger: 'change' }
  ],
  project_id: [
    { required: true, message: '请选择所属项目', trigger: 'change' }
  ],
  steps_to_reproduce: [
    { required: true, message: '请输入重现步骤', trigger: 'blur' },
    { min: 10, max: 1000, message: '重现步骤长度应在 10 到 1000 个字符', trigger: 'blur' }
  ],
  expected_result: [
    { required: true, message: '请输入预期结果', trigger: 'blur' },
    { max: 500, message: '预期结果长度不能超过 500 个字符', trigger: 'blur' }
  ],
  actual_result: [
    { required: true, message: '请输入实际结果', trigger: 'blur' },
    { max: 500, message: '实际结果长度不能超过 500 个字符', trigger: 'blur' }
  ]
} as const

// 缺陷列表表格列配置
export const DEFECT_TABLE_COLUMNS = [
  { prop: 'title', label: '标题', minWidth: 200, showOverflowTooltip: true },
  { prop: 'type', label: '类型', width: 100 },
  { prop: 'status', label: '状态', width: 100 },
  { prop: 'priority', label: '优先级', width: 100 },
  { prop: 'severity', label: '严重程度', width: 120 },
  { prop: 'assignee_name', label: '负责人', width: 120 },
  { prop: 'reporter_name', label: '报告人', width: 120 },
  { prop: 'project_name', label: '所属项目', width: 150 },
  { prop: 'found_date', label: '发现日期', width: 120 },
  { prop: 'created_at', label: '创建时间', width: 150 }
] as const

// 缺陷筛选器默认值
export const DEFECT_FILTER_DEFAULTS = {
  page: 1,
  size: 20,
  sort_by: 'created_at',
  sort_order: 'desc' as const
}

// 缺陷统计图表配置
export const DEFECT_CHART_COLORS = {
  status: {
    new: '#909399',
    assigned: '#E6A23C',
    in_progress: '#409EFF',
    resolved: '#67C23A',
    verified: '#67C23A',
    closed: '#909399',
    reopened: '#F56C6C'
  },
  priority: {
    lowest: '#C0C4CC',
    low: '#909399',
    medium: '#E6A23C',
    high: '#F56C6C',
    urgent: '#F56C6C',
    critical: '#F56C6C',
    blocker: '#F56C6C'
  },
  severity: {
    trivial: '#C0C4CC',
    minor: '#909399',
    major: '#E6A23C',
    important: '#F56C6C',
    critical: '#F56C6C',
    urgent: '#F56C6C',
    blocker: '#F56C6C'
  }
} as const

// 工具函数：根据值获取选项标签
// 注意：这些函数已迁移到 composables，保留此文件仅为向后兼容
// 建议使用 useDefectStates() 和 useDefectOptions() composables

/**
 * @deprecated 请使用 useDefectStates().getStatusName() 替代
 */
export const getDefectStatusLabel = (value: string) => {
  return DEFECT_STATUS_OPTIONS.find(option => option.value === value)?.label || value
}

/**
 * @deprecated 请使用 useDefectOptions().getPriorityName() 替代
 */
export const getDefectPriorityLabel = (value: string) => {
  return DEFECT_PRIORITY_OPTIONS.find(option => option.value === value)?.label || value
}

/**
 * @deprecated 请使用 useDefectOptions().getSeverityName() 替代
 */
export const getDefectSeverityLabel = (value: string) => {
  return DEFECT_SEVERITY_OPTIONS.find(option => option.value === value)?.label || value
}

export const getDefectTypeLabel = (value?: string | null) => {
  if (!value) return ''
  return DEFECT_TYPE_OPTIONS.find(option => option.value === value)?.label || value
}

export const getDefectSourceLabel = (value?: string | null) => {
  if (!value) return ''
  return DEFECT_SOURCE_OPTIONS.find(option => option.value === value)?.label || value
}

// 工具函数：根据值获取选项颜色
export const getDefectStatusColor = (value: string) => {
  return DEFECT_STATUS_OPTIONS.find(option => option.value === value)?.color || '#909399'
}

export const getDefectPriorityColor = (value: string) => {
  return DEFECT_PRIORITY_OPTIONS.find(option => option.value === value)?.color || '#909399'
}

export const getDefectSeverityColor = (value: string) => {
  return DEFECT_SEVERITY_OPTIONS.find(option => option.value === value)?.color || '#909399'
}

// 工具函数：获取优先级类型（用于 Element Plus tag 的 type 属性）
/**
 * @deprecated 请使用 useDefectOptions().getPriorityType() 替代
 */
export const getDefectPriorityType = (priority: string): 'success' | 'primary' | 'warning' | 'info' | 'danger' => {
  const priorityMap: Record<string, 'success' | 'primary' | 'warning' | 'info' | 'danger'> = {
    'lowest': 'info',
    'low': 'info',
    'medium': 'warning',
    'high': 'danger',
    'urgent': 'danger',
    'critical': 'danger',
    'blocker': 'danger'
  }
  return priorityMap[priority.toLowerCase()] || 'info'
}

// 工具函数：获取严重程度类型（用于 Element Plus tag 的 type 属性）
/**
 * @deprecated 请使用 useDefectOptions().getSeverityType() 替代
 */
export const getDefectSeverityType = (severity: string): 'success' | 'primary' | 'warning' | 'info' | 'danger' => {
  const severityMap: Record<string, 'success' | 'primary' | 'warning' | 'info' | 'danger'> = {
    'trivial': 'info',
    'minor': 'primary',
    'major': 'warning',
    'important': 'danger',
    'critical': 'danger',
    'urgent': 'danger',
    'blocker': 'danger'
  }
  return severityMap[severity.toLowerCase()] || 'info'
}

// 工具函数：检查状态转换是否允许 - 已迁移到状态管理系统
// 使用状态管理系统中的状态流转验证

// 工具函数：获取允许的状态转换 - 已迁移到状态管理系统
// 使用状态管理系统中的状态流转规则